#include "Piece.h"
#include "Block.h"
#include "Configuration.h"
#include "UpdatePieceNetworkEvent.h"
#include "AddBlockToPieceNetworkEvent.h"
#include "LinearAnimation.h"

Piece::Piece(long id, const Position& position, const Color& color)
        : GraphicalComposite(id), color(color)
{
    setPosition(position.scale(Configuration::BLOCK_SIZE));
}

void Piece::handleEvent(const Event& event)
{
    event.visitPiece(this);
}

void Piece::handleUpdatePieceEvent(long pieceId, const Position& position)
{
    if (pieceId == getId())
    {
        Position newPosition = position.scale(Configuration::BLOCK_SIZE);
        setAnimation(new LinearAnimation(getPosition(), newPosition, 100));
    }
}

void Piece::handleAddBlockToPieceEvent(long pieceId, long blockId, const Position& position)
{
    if (pieceId == getId())
    {
        addBlock(blockId, position);
    }
}

void Piece::addBlock(long blockId, const Position& position)
{
    addChild(new Block(blockId, position, color));
}

Piece::~Piece()
{
}
